clear all
set more off
capture log close
log using "$output/figure5.log", replace

version 16	

use "$data/ctctaxsim_mar1kid1.dta", clear
append using "$data/ctctaxsim_mar1kid2.dta"
append using "$data/ctctaxsim_mar1kid3.dta"
tab depx

sum fiitax earnings fedtaxlessctc v25 totctc
replace earnings=earnings*1000
replace ctc=ctc*1000
replace v25=v25*1000
replace totctc=totctc*1000
sum fiitax earnings fedtaxlessctc v25 totctc

gen avgtaxrate=fiitax/earnings
gen avgtaxrate2021=fedtaxlessctc/earnings	/* Don't include new CTC */

gen avgtaxeitc=-v25/earnings
gen avgtaxctc=-totctc/earnings
gen avgtaxctc2021=0

gen avgtaxeitcctc=avgtaxeitc+avgtaxctc

*br avgtaxrate avgtaxeitcctc avgtaxeitc avgtaxctc earnings year depx if year==2018
*br avgtaxrate avgtaxeitcctc avgtaxeitc avgtaxctc earnings year depx avginctax if year==2017
gen avginctax=avgtaxrate- avgtaxeitcctc

tab year avginctax if earnings==40000 & depx==1
replace avginctax=avginctax*.04/.037875 if year==2017
tab year avginctax if earnings==40000 & depx==1

foreach var in avgtaxeitc avgtaxctc avgtaxctc2021 avginctax{
	replace `var'=-`var'
}

foreach var in avgtaxeitc avgtaxctc avgtaxctc2021 avginctax{
	replace `var'=`var'+1
}

replace avgtaxrate=-avgtaxrate+1
replace avgtaxrate2021=-avgtaxrate2021+1

twoway line avgtaxrate earnings if year==2017 & depx==1, lp(-) pstyle(p1) || line avgtaxrate earnings if year==2018 & depx==1, lp(_) pstyle(p2) || line avgtaxrate2021 earnings if year==2018 & depx==1, pstyle(p1) graphregion(color(white)) bgcolor(white) leg(order(1 "2017" 2 "2020" 3 "2021") c(1) pos(2) ring(0)) ytitle("Net Return to Work") xtitle("Earnings" "{bf:Panel A: One Child}")  ylabel(.75(.25)1.5)
graph save "$output/avgtax_mar1kid1.gph", replace

twoway line avgtaxrate earnings if year==2017 & depx==2, lp(-) pstyle(p1) || line avgtaxrate earnings if year==2018 & depx==2, lp(_) pstyle(p2) || line avgtaxrate2021 earnings if year==2018 & depx==2, pstyle(p1) graphregion(color(white)) bgcolor(white) leg(order(1 "2017" 2 "2020" 3 "2021") c(1) pos(2) ring(0)) ytitle("Net Return to Work") xtitle("Earnings" "{bf:Panel B: Two Children}")  ylabel(.75(.25)1.5)
graph save "$output/avgtax_mar1kid2.gph", replace

twoway line avgtaxrate earnings if year==2017 & depx==3, lp(-) pstyle(p1) || line avgtaxrate earnings if year==2018 & depx==3, lp(_) pstyle(p2) || line avgtaxrate2021 earnings if year==2018 & depx==3, pstyle(p1) graphregion(color(white)) bgcolor(white) leg(order(1 "2017" 2 "2020" 3 "2021") c(1) pos(2) ring(0)) ytitle("Net Return to Work") xtitle("Earnings" "{bf:Panel C: Three Children}")  ylabel(.75(.25)1.5)
graph save "$output/avgtax_mar1kid3.gph", replace

graph combine "$output/avgtax_mar1kid1.gph" "$output/avgtax_mar1kid2.gph" "$output/avgtax_mar1kid3.gph" ,graphregion(color(white)) 
graph export "$output/figure5.png", replace

erase "$output/avgtax_mar1kid1.gph" 
erase "$output/avgtax_mar1kid2.gph" 
erase "$output/avgtax_mar1kid3.gph"

log close